package com.visionet.order.service.impl;

import com.visionet.order.entity.*;
import com.visionet.order.repository.OrderRepository;
import com.visionet.order.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * @Author: pengjx
 * @Date: 2019-01-22 14:33
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderRepository orderRepository;


    public String saveOrder(String id) {
        boolean sfbhbx = false;
        Order order = new Order();
        PlaneTicketOrder planeTicketOrder = new PlaneTicketOrder();
        //==============保存主订单表数据==============
        log.info("---开始保存主订单信息---");
        order.setCreateTime(new Date());
        String orderId = "peng_test20190122" + id;
        order.setOrder_id(orderId);
        order.setOrderStatus(OrderStatus.PENDING);
        order.setPrice(new BigDecimal(100));
        if (sfbhbx) {
            order.setOrderType(OrderType.airticket_insurance);
        } else {
            order.setOrderType(OrderType.airticket);
        }
        //==============保存机票订单表数据==============
        log.info("---开始保存机票订单信息---");
        planeTicketOrder.setBaseOrderId(orderId);
        planeTicketOrder.setCw("商务舱");
        planeTicketOrder.setHbh("HA7892");
        planeTicketOrder.setPslx("机场自取");
        planeTicketOrder.setLxrxm("联系人姓名");
        planeTicketOrder.setLxrsj("18000000000");
        //保存乘机人信息
        log.info("---开始保存机票乘机人信息---");
        Consumer consumer = new Consumer();
        consumer.setEmbedOrderId(orderId);
        if (sfbhbx) {
            consumer.setDesc("机票乘机人");
        } else {
            consumer.setDesc("机票乘机人+保险投被保人");
        }
        consumer.setName("乘机人");
        consumer.setAddr("上海市普陀区大渡河路168号");
        consumer.setIdType(IDType.IDENTITYCARD);
        consumer.setIdNo("410381199112241533");

        //==============保存保险订单表数据==============
        if (sfbhbx) {
            log.info("---开始保存保险订单信息---");
            //如果包含保险，那么同时保存保险订单
            InsuranceOrder insuranceOrder = new InsuranceOrder();
            insuranceOrder.setBaseOrderId(orderId);
            insuranceOrder.setAmount(new BigDecimal(10000.00));
            insuranceOrder.setProductCode("HYX0001");
            insuranceOrder.setProductName("航意险");
            insuranceOrder.setInceptionDate(new Date());
        }
        return "success";
    }
}
